package com.zzptc.lzh.myapp26;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.zzptc.lzh.myapp26.Bean.User;

import org.litepal.LitePal;

import java.util.List;

public class LoginActivity extends AppCompatActivity {
    EditText yhm,mm;//全局变量
    private RadioGroup radioGroup;
    Button dl,zc;
    int CHOOSE_LEVEL=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        User user=new User(0,"admin","admin",2);
        user.save();
        initView();
        setListener();
    }

    private void setListener() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.userdb:
                        CHOOSE_LEVEL=1;
                        break;
                    case R.id.adminrdb:
                        CHOOSE_LEVEL=2;
                        break;
                }
            }
        });
        dl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String usn=yhm.getText().toString().trim();
                String psw=mm.getText().toString().trim();
                User user=confirmUser(usn,psw);
                if (user==null){
                    showToast("账号密码都记不住吗？");
                }else if (user.getPermission()==CHOOSE_LEVEL){
                    SharedPreferences sp=getSharedPreferences("userinfo",MODE_PRIVATE);
                    SharedPreferences.Editor editor=sp.edit();
                    editor.putInt("userid",user.getId());
                    editor.putString("username",user.getName());
                    editor.putString("password",user.getPassword());
                    editor.putInt("permission",user.getPermission());
                    editor.apply();
                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                }else{
                    showToast("权限选择错误");
                }
            }
        });

        zc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String usn=yhm.getText().toString().trim();
                String psw=mm.getText().toString().trim();
                User user=new User(0,usn,psw);
                if (user.save()){
                    showToast("注册成功");
                }else{
                    showToast("不要乱打");
                }
            }
        });
    }
    private void showToast(String str){
        Toast.makeText(LoginActivity.this, str, Toast.LENGTH_SHORT).show();
    }
    private User confirmUser(String usn,String psw){
        List<User> userList= LitePal.findAll(User.class);
        for (User u:userList){
            //String形判断值不能用“==”方法
            if (usn.equals(u.getName())&&psw.equals(u.getPassword())){
                return u;//打断循环
            }
        }
        return null;
    }

    private void initView() {
        yhm=findViewById(R.id.yhm);//实例化控件
        mm=findViewById(R.id.mm);
        dl=findViewById(R.id.dl);
        zc=findViewById(R.id.zc);
        radioGroup=findViewById(R.id.radiogroup);
    }
}
